#include <SFML/Graphics.hpp>
#include <iostream>
#include "gameSprite.h"
#include "resourceManager.h"
#include "tileSelector.h"
#include "spriteBox.h"
#include "enums.h"

sf::Vector2f getRealMousePos(sf::RenderWindow* App, int ZOOM_FACTOR)
{
	sf::Mouse mouse;
	return sf::Vector2f(mouse.getPosition(*App).x/ZOOM_FACTOR, mouse.getPosition(*App).y/ZOOM_FACTOR);
}

int main()
{
	const int SCREEN_WIDTH = 128 + 16;
	const int SCREEN_HEIGHT = 128 + 16;
	const int ZOOM_FACTOR = 3;
	
	// Create the main rendering window
	sf::RenderWindow App(sf::VideoMode(SCREEN_WIDTH*ZOOM_FACTOR, SCREEN_HEIGHT*ZOOM_FACTOR, 32), "Map Test");
	sf::View mainView(sf::FloatRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
	App.setView(mainView);

	sf::Clock clock;
	sf::Keyboard key;

	App.setFramerateLimit(60);

	sf::Color bgColor;
	bgColor.r = 90;
	bgColor.g = 100;
	bgColor.b = 110;

	resourceManager r_man;
	r_man.addTexture("tileset", "images\\tileset.png");
	r_man.addTexture("fancybox", "images\\box.png");

	tileSelector t_selector;
	t_selector.setPosition(8, 8);
	t_selector.setTileSheet(r_man.getTexture("tileset"));
	t_selector.setBorderSheet(r_man.getTexture("fancybox"));

	// Start game loop
	while (App.isOpen())
	{
		// Process events
		sf::Event Event;
		while (App.pollEvent(Event))
		{
			// Close window : exit
			if (Event.type == sf::Event::Closed)
				App.close();
			if (Event.type == sf::Event::MouseButtonPressed)
				t_selector.processClick(getRealMousePos(&App, ZOOM_FACTOR).x, getRealMousePos(&App, ZOOM_FACTOR).y);
			if (Event.type == sf::Event::MouseMoved)
				t_selector.setViewBoxPosition(getRealMousePos(&App, ZOOM_FACTOR).x, getRealMousePos(&App, ZOOM_FACTOR).y);
		}

		// Get elapsed time per frame
		float ElapsedTime = clock.restart().asSeconds();

		// Clear screen
		App.clear(bgColor);

		t_selector.draw(&App);

		// Display window contents on screen
		App.display();
	}

	return EXIT_SUCCESS;
}